<!DOCTYPE HTML>
<html lang="cn" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
  <%include("/common/header.html",{title:''}){}%>
  <title>轮播图管理</title>
  <style>
  </style>
</head>
<body>

<div class="x-nav" style="margin: 20px auto auto 15px;">
      <span class="layui-breadcrumb" lay-filter="breadcrumb" style="visibility: visible;">
        <a href="javascript:;">首页</a><span lay-separator="">/</span>
        <a href="javascript:;">广告理</a><span lay-separator="">/</span>
        <a><cite>广告位管理</cite></a>
      </span>
  <a class="layui-btn layui-btn-primary layui-btn-small" style="margin: -5px 14px 0px 0px;float:right;"
     href="javascript:location.replace(location.href);" title="刷新">
    <i class="layui-icon layui-icon-refresh-3"></i>
  </a>
</div>


<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-card-body">
      <table id="LAY-user-manage" lay-filter="LAY-user-manage"></table>
      <script type="text/html" id="toolbarDemo">
        <button class="layui-btn layuiadmin-btn-useradmin" lay-event="delete">删除</button>
        <button class="layui-btn layuiadmin-btn-useradmin" lay-event="add">添加</button>
      </script>
      <script type="text/html" id="imgTpl">
        <img style="display: inline-block; width: 50%; height: 100%;" src={{ d.avatar }}>
      </script>
      <script type="text/html" id="table-useradmin-webuser">
        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
      </script>
    </div>
  </div>
</div>
<!-- 弹出页面 -->
<div class="layui-form" lay-filter="layuiadmin-form-useradmin"
     id="layuiadmin-form-useradmin"
     style="padding: 15px;display: none">
  <form action class="layui-form" lay-filter="component-form-element" id="myform">
    <input type="hidden" name="id">

    <div class="layui-form-item">
      <div class="layui-input-inline">
        <input style="width: 555px;" type="text" name="title" lay-verify="required" value="" class="layui-input"
               placeholder="请输入广告位标题（必填）">
      </div>
    </div>

    <div class="layui-form-item">
      <label class="layui-form-label" style="padding-left: 0px;font-size: medium;width: auto;">请输入广告位内容:</label>
      <div class="layui-input-inline" style="width: 555px">
        <textarea id="content" type="text" style="display: none" name="content" lay-verify="content"></textarea>
      </div>
    </div>

    <div class="layui-form-item">
      <div class="layui-inline">
        <div class="layui-input-inline">
          <button type="button" class="layui-btn" id="upload1">上传广告位图片</button>
          <div class="layui-upload-list">
            <img class="layui-upload-img" width="100px" height="80px" id="img1"/>
            <p id="demoText"></p>
          </div>
        </div>
      </div>
    </div>

    <div class="layui-form-item">
      <div class="layui-input-block" style="margin-left: 240px;">
        <button class="layui-btn" lay-submit="" lay-filter="component-form-element">立即提交</button>
      </div>
    </div>
  </form>
</div>

<%include("/common/foot.html"){}%>
<script>
  layui.use(['index', 'table', 'form', 'laydate', 'layedit', 'AjaxCommon', 'AliangLayui'], function () {
    var $ = layui.$;
    var table = layui.table;
    var form = layui.form;
    var laydate = layui.laydate;
    var AjaxCommon = layui.AjaxCommon;
    var AliangLayui = layui.AliangLayui;

    var title = "添加广告位";
    var size = ['600px', '500px']
    var findAll_url = 'system/advertisement/findAll.json';
    var update_url = 'system/advertisement/update.jhtml';
    var delete_url = 'system/advertisement/delete.jhtml';
    var index;//定义建立富文本编辑器全局变量
    var layedit//定义富文本编辑器layedit全局变量
    var cols = [ //表头
      {checkbox: true, fixed: true},
      {
        field: 'createDate',
        width: "150",
        templet: "<div>{{layui.util.toDateString(d.time, 'yyyy-MM-dd ')}}</div>",
        title: '发布时间',
        fixed: 'left',
      },
      {
        field: 'image',
        width: "110",
        title: '广告缩略图',
        templet: "<div><img src={{d.image}} style='width:30px;height:30px'></div>"
      },
      {title: '操作', fixed: 'right', width: 165, toolbar: '#table-useradmin-webuser',}
    ];
    AliangLayui.createTable(
            "#LAY-user-manage",
            "listReload",
            "#toolbarDemo",
            title,
            findAll_url,
            true,
            [10, 15, 20, 25],
            ['filter', 'print', 'exports'],
            cols,
            null
    );



    //初始化修改页面

    function loadData(data) {
      var ret = {
        "id": data.id
      }
      $("#img1").attr("src", data.image);
      return ret;
    }

    //图片上传
    AliangLayui.layuiImagepost("#upload1", "#img1");
    //初始化日期模块
    laydate.render({
      elem: '#createDate' //指定元素
    });
    //////////////////////////////////////以上是修改每个页面修改的地方
    //弹出修改页面调用初始化数据方法
    function initUpdate(data) {
      AliangLayui.openModel(1, $('#layuiadmin-form-useradmin'), size, title, ['关闭'], function () {
        location.replace(location.href);　//layer.open关闭刷新
      });
      //富文本编辑器
      layedit = layui.layedit;
      //建立编辑器
      index = layedit.build('content', {
        tool: ['strong', 'italic', 'underline', 'del', '|',
          'left', 'center', 'right', '|',
          'link', 'unlink',],
        height: 280
      });
      //富文本表单验证
      form.verify({
        content: function (value) {
          var val = layedit.getText(index);
          //alert(val.length)
          if (val.length < 6) {

            return '内容至少6个字符'
          }
        }
      });
      //如果是添加则重置表单，如果是编辑则给表单赋值
      if (data == 0 || data == null) {
        $('#myform')[0].reset();
        return;
      } else {
        layui.form.val("component-form-element", loadData(data));
      }
    }

    //修改页面的内容提交到后台处理
    form.on('submit(component-form-element)', function (data) {
      data.field.content = layedit.getContent(index);
      postUpdate(data);
      return false;
    });
    //头工具栏事件
    table.on('toolbar(LAY-user-manage)', function (obj) {
      var checkStatus = table.checkStatus(obj.config.id);
      switch (obj.event) {
        case 'add':
          initUpdate(0);
          break;
        case 'delete':
          var data = checkStatus.data;
          if (data.length > 0) {
            //var id = new Array(); //声明数组
            for (var i in data) {
              var a = data[i].id; //得到复选框的值
              postDelete(a);
            }
          } else {
            layer.msg("至少选择一个进行删除");
          }
          break;
        case 'export':
          layer.msg("导出");
          break;
      }
    });

    //点击每行右工具栏“操作”里面按钮效果
    table.on('tool(LAY-user-manage)', function (obj) {
      var data = obj.data;
      if (obj.event === 'detail') {
        layer.msg('ID：' + data.id + ' 的查看操作');
      } else if (obj.event === 'edit') {
        initUpdate(data);
      } else if (obj.event === 'del') {
        layer.confirm('删除以后数据将不能恢复，确定删除吗？', function (index) {
          postDelete(data.id);
        });
      }
    });

    //ajax提交删除内容
    window.postDelete = function (id) {
      AjaxCommon.ajax(delete_url, "post", "json", {"id": id}, function (ret) {
        if (ret == null) {
          layer.msg("删除失败", {
            icon: 5
          });
        } else {
          layer.msg("删除成功", {
            icon: 6
          });
          layer.closeAll();
          table.reload('listReload');
        }
        return ret;
      });
    };

    //ajax提交修改内容
    window.postUpdate = function (data) {
      var formData = new FormData();
      formData.append("data", JSON.stringify(data.field));
      formData.append("file", $("input[name='file']")[0].files[0]);
      $.ajax({
        url: update_url,
        type: "POST",
        //traditional:true,
        data: formData,
        processData: false,
        contentType: false,
        success: function (data) {
          if (data.code == 0) {
            layer.closeAll();
            layer.msg(data.msg, {time: 600, icon: 1}, function () {
              //table.reload('listReload');
              location.replace(location.href);
            });
          } else {
            layer.msg(data.msg, {icon: 5});
          }
        },
        error: function (jqXHR, textStatus, errorThrown) {
          var estr = "Page:" + window.location.pathname + ",Function:postUpdate()";
          layer.open({
            title: "错误信息",
            type: 2,
            content: "base/error.jhtml?info=" + estr,
            btn: ['关闭'] //可以无限个按钮
          });
        }
      });
    }
  });

</script>
</body>

</html>